package com.ww.springboot.boot.algorithm.leetcode1;

import java.util.*;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2022-05-31 09:33
 */
public class CG046二叉树的右侧视图 {

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1,new TreeNode(2,null,new TreeNode(5)),new TreeNode(3,null,new TreeNode(4)));
        System.out.println(rightSideView(root));
    }

    public static List<Integer> rightSideView(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root == null){
            return list;
        }
        Integer num = null;
        Queue<TreeNode> treeNodeStack = new ArrayDeque<>();
        treeNodeStack.add(root);
        treeNodeStack.add(new TreeNode(101));
        while (!treeNodeStack.isEmpty()) {
            TreeNode treeNode = treeNodeStack.remove();
            if (treeNode.val == 101) {
                list.add(num);
                if (treeNodeStack.isEmpty()) {
                    break;
                }
                treeNodeStack.add(new TreeNode(101));
                continue;
            } else {
                if (treeNode.left != null) {
                    treeNodeStack.add(treeNode.left);
                }
                if (treeNode.right != null) {
                    treeNodeStack.add(treeNode.right);
                }
            }
            num = treeNode.val;
        }
        return list;
    }


    public static List<Integer> rightSideView2(TreeNode root) {
        Map<Integer, Integer> rightmostValueAtDepth = new HashMap<Integer, Integer>();
        int max_depth = -1;

        Queue<TreeNode> nodeQueue = new ArrayDeque<TreeNode>();
        Queue<Integer> depthQueue = new ArrayDeque<Integer>();
        nodeQueue.add(root);
        depthQueue.add(0);

        while (!nodeQueue.isEmpty()) {
            TreeNode node = nodeQueue.remove();
            int depth = depthQueue.remove();

            if (node != null) {
                // 维护二叉树的最大深度
                max_depth = Math.max(max_depth, depth);

                // 由于每一层最后一个访问到的节点才是我们要的答案，因此不断更新对应深度的信息即可
                rightmostValueAtDepth.put(depth, node.val);

                if(node.left != null){
                    nodeQueue.add(node.left);
                    depthQueue.add(depth + 1);
                }
                if(node.right != null){
                    nodeQueue.add(node.right);
                    depthQueue.add(depth + 1);
                }
            }
        }

        List<Integer> rightView = new ArrayList<Integer>();
        for (int depth = 0; depth <= max_depth; depth++) {
            rightView.add(rightmostValueAtDepth.get(depth));
        }

        return rightView;
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
